--
-- Author: gnar
-- Date: 2016-01-17 17:48:17
--

--服务器要做的就是 收到消息 然后决定转发人群 原样转发出去
local ManagerChatData = class("ManagerChatData")
local gInstance = nil

ManagerChatData.maxChatItemNum = 60
ManagerChatData.TextWinWidth = 400

ManagerChatData.channels = 
{
[0] = "total",
"system",
"current",
"world",
"team",
"menpai",
"union",
total = 0,
system = 1,
current = 2,
world = 3,
team = 4,
menpai = 5,
union = 6,
}


--chatItem = {channelIdx,senderName,msg}
function ManagerChatData.getInstance()
	if not gInstance then
		gInstance = ManagerChatData.new()
	end

	return gInstance
end

function ManagerChatData:ctor()
	for i=0, 6 do
		self[ ManagerChatData.channels[i] .. "List"] = {}
	end

	--转发到特定人群的广播由服务器过滤
    --self.personChatList = {} --私人聊天单独界面 就和梦幻一样
    self.currentOpenedChannel = ManagerChatData.channels.system
end

function ManagerChatData:sendChat(chatItem)  --在这里会过滤 系统频道不会调用AdapterSendMsg:sendMsg  只会做下数据维护

    if ManagerChatData.channels[chatItem.channelIdx] == "system" then  --在系统频道发言不会传服务器
    	self:insertChatItem(chatItem, ManagerChatData.channels.total)  
    	self:insertChatItem(chatItem)  
    	ManagerNotification:post("chat.newChat",chatItem)
    else
    	--服务器会返回自己的发言 
    	AdapterSendMsg:sendMsg("chat.sendChat",chatItem)
    end

end

function ManagerChatData:getChatListByType(channelIdx)  

    local targetList = self[ ManagerChatData.channels[channelIdx] .. "List" ]
    return targetList

end

function ManagerChatData:insertChatItem(chatItem, channelIdx)  

    channelIdx = channelIdx or chatItem.channelIdx
    local targetList = self:getChatListByType(channelIdx)  
    local encodeMsg = TextPaser.attachSrcForMsg(chatItem.msg, chatItem.channelIdx, chatItem.senderName)
    local rich  = TextPaser.encodeMsg2RichText(encodeMsg, ManagerChatData.TextWinWidth, true)
    rich:formatText()
    local height = rich:getContentSize().height
    chatItem.height = height
    if table.nums(targetList) >= ManagerChatData.maxChatItemNum then
        table.remove(targetList, 1)
        table.insert(targetList, chatItem)
    else
        table.insert(targetList, chatItem)
    end 

end




return ManagerChatData